<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 社区充值管理器
 */
class Recharge extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('bbs_account_model');
        $this->load->model('bbs_recharge_model');
        $this->load->model('bbs_detail_model');
        $this->load->library('Alipay');
        //支付宝配置信息
        $this->alipay_config = $this->web_config['ALIPAY'];
    }
    
    function index(){
        $this->assign("rect_url", $_SERVER['HTTP_REFERER']);
        $this->display("recharge/index.html");
    }
    //处理充值订单
    function action_order(){
        
        if($_POST){
            
            if(!$this->userinfo){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            
            $key = isset($_POST['rechargeKey']) ? trim($_POST['rechargeKey']) : "";
            $num = isset($_POST['rechargeNum']) ? trim($_POST['rechargeNum']) : 0;
            $pay = isset($_POST['rechargePay']) ? trim($_POST['rechargePay']) : "";
            $orderIdIndex = isset($_POST['orderIdIndex']) ? trim($_POST['orderIdIndex']) : "";
            if($key == "" || $key != $this->session_id){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            
            if(!$num){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "充值溜珠的个数需≥1";
                echo JSON($mess);exit;
            }
            
            if(!$pay || ($pay != "alipay" && $pay != "weixin")){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "请选择支付方式";
                echo JSON($mess);exit;
            }
            
            if(!$orderIdIndex){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "请求参数有误！";
                echo JSON($mess);exit;
            }
            
            $user_id = $this->bbs_userinfo['user_id'];
            $order_nid = MakeOrdId($user_id);
            
            //订单数据
            $data = array();
            $data['order_nid'] = $order_nid;
            $data['order_index'] = $orderIdIndex;
            $data['user_id'] = $user_id;
            $data['product_name'] = $num."个溜珠";
            $data['product_money'] = sprintf("%0.2f",$num);
            $data['product_num'] = $num;
            $data['recharge_type'] = $pay;
            $data['status'] = 0;
            $data['create_time'] = time();
            //创建充值订单表
            $order_id = $this->bbs_recharge_model->insert_recharge_order($data);
            if($order_id){
                //创建充值订单副表
                $this->bbs_recharge_model->insert_recharge_list($data);
                if($pay == "alipay"){
                    
                    $order = $this->bbs_recharge_model->get_recharge_order(array("order_nid"=>$order_nid));
                    echo "正在跳转支付页面...";
                    //构造请求参数
                    $args = array();
                    $args['number'] = $order['order_nid'];
                    $args['name'] = $order['product_name'];
                    $args['total'] = $order['product_money'];
                    $args['content'] = "";
                    $args['notify_url'] = web_url("recharge_notify");
                    $args['return_url'] = web_url("recharge_result");
                    $alipay = new Alipay();
                    $alipay->recharge($this->alipay_config, $args);exit;
                }
                
                if($pay == "weixin"){
                    $mess['rechargeUrl'] = web_url("do_weixin_recharge");
                }
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "操作失败，请稍后重试！";
                echo JSON($mess);exit;
            }
            
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1001;
            $mess['errorMsg'] = "非法请求";
            echo JSON($mess);exit;
        }
    }
    
    //继续支付请求
    function action_order_continus(){
        if($_POST){
            
            if(!$this->userinfo){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            
            
            $pay = isset($_POST['rechargePay']) ? trim($_POST['rechargePay']) : "";
            $orderIdIndex = isset($_POST['orderIdIndex']) ? trim($_POST['orderIdIndex']) : "";
            
            
            if(!$pay || ($pay != "alipay" && $pay != "weixin")){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "请求参数有误！";
                echo JSON($mess);exit;
            }
            
            if(!$orderIdIndex){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "请求参数有误！";
                echo JSON($mess);exit;
            }
            
            $order = $this->bbs_recharge_model->get_recharge_order(array("order_index"=>$orderIdIndex));
            if($order['status'] == 1){
                $this->assign("errorStatus",0);
                $this->assign("errorMsg", $order['product_num']);
                $this->display("recharge/recharge_result.html");exit;
            }
            
            if($pay == "alipay"){
                
                echo "正在跳转支付页面...";
                //构造请求参数
                $args = array();
                $args['number'] = $order['order_nid'];
                $args['name'] = $order['product_name'];
                $args['total'] = $order['product_money'];
                $args['content'] = "";
                $args['notify_url'] = web_url("recharge_notify");
                $args['return_url'] = web_url("recharge_result");
                $alipay = new Alipay();
                $alipay->recharge($this->alipay_config, $args);exit;
            }

            if($pay == "weixin"){
                $mess['rechargeUrl'] = web_url("do_weixin_recharge");
            }
        }else{
            web_redirect("");
        }
    }
    //充值-页面跳转同步通知页面
    function recharge_result(){
        header("Content-type: text/html; charset=utf-8"); 
        $errorStatus = 0;$errorMsg = "";
        if($_GET){
            //计算得出通知验证结果
            $alipayNotify = new AlipayNotify($this->alipay_config);
            $verify_result = $alipayNotify->verifyReturn();
            
            if($verify_result) {//验证成功
                //商户订单号
                $out_trade_no = $_GET['out_trade_no'];
                if(!$out_trade_no){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1001;
                    $mess['errorMsg'] = "请求参数有误！";
                    echo JSON($mess);exit;
                }
                //支付宝交易号
                $trade_no = $_GET['trade_no'];
                //交易状态
                $trade_status = $_GET['trade_status'];

                $data = array();
                $data['trade_nid'] = $trade_no;
                $data['trade_status'] = $trade_status;
                $data['update_time'] = time();
                //获取订单信息
                $order = $this->bbs_recharge_model->get_recharge_order(array("order_nid"=>$out_trade_no));
                if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
                    if($order){
                        $data['status'] = 1;
                        if($order['status'] == 0){
                            if($this->bbs_recharge_model->update_recharge_order($data,$order['order_nid'])){
                                
                                $this->db->insert("bbs_zhu_detail",array("user_id"=>$order["user_id"],"num"=>$order["product_num"],"type"=>"充值","style"=>1,"create_time"=>time()));
                                
                                //更新社区个人账户信息
                                $bbs_account = $this->bbs_account_model->get($order["user_id"]);
                                $bbs_account_data = array();
                                $bbs_account_data['total'] = $bbs_account['total'] + $order['product_num'];
                                $bbs_account_data['balance'] = $bbs_account['balance'] + $order['product_num'];
                                $this->bbs_account_model->update_bbs_account($bbs_account_data,$order['user_id']);
                                //更新充值订单附表
                                $this->bbs_recharge_model->update_recharge_list($data,$order['order_nid']);

                                $errorStatus = 0;
                                $errorMsg = $order['product_num'];
                            }else{
                                $errorStatus = 1;
                                $errorMsg = "操作失败，是稍后重试！";
                            }
                        }else{
                            $errorStatus = 0;
                            $errorMsg = $order['product_num'];
                        }
                        
                    }else{
                        $errorStatus = 1;
                        $errorMsg = "操作失败，原因：订单不存在！";
                    }
                    
                }else{
                    $errorStatus = 1;
                    $errorMsg =  "操作异常，异常状态码：".$_GET['trade_status'];
                }

            }else {
                $errorStatus = 1;
                $errorMsg =  "操作失败，原因：验签失败！";
            }
            
        }else{
            
        }
        $this->assign("errorStatus",$errorStatus);
        $this->assign("errorMsg", $errorMsg);
        $this->display("recharge/recharge_result.html");
    }
    // 充值-服务器异步通知页面路径
    function recharge_notify(){
        if($_POST){
            //计算得出通知验证结果
            $alipayNotify = new AlipayNotify($this->alipay_config);
            $verify_result = $alipayNotify->verifyNotify();
            if($verify_result) {//验证成功
                //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
                //商户订单号
                $out_trade_no = $_POST['out_trade_no'];
                //支付宝交易号
                $trade_no = $_POST['trade_no'];
                //交易状态
                $trade_status = $_POST['trade_status'];

                //获取订单信息
                $order = $this->bbs_recharge_model->get_recharge_order(array("order_nid"=>$out_trade_no));
                
                if(!$order){
                    echo "fail";exit;
                }
                
                if($order['status'] == 1){//订单已经处理过了
                    echo "success";exit ;
                }
                $data = array();
                $data['trade_nid'] = $trade_no;
                $data['trade_status'] = $trade_status;
                $data['update_time'] = time();
                if($_POST['trade_status'] == 'TRADE_FINISHED' || $_POST['trade_status'] == 'TRADE_SUCCESS') {
                    $data['status'] = 1;
                    //更改充值订单状态
                    if($this->bbs_recharge_model->update_recharge_order($data,$order['order_nid'])){
                        
                        $this->db->insert("bbs_zhu_detail",array("user_id"=>$order["user_id"],"num"=>$order["product_num"],"type"=>"充值","style"=>1,"create_time"=>time()));
                        //更新社区个人账户信息
                        $bbs_account = $this->bbs_account_model->get($order["user_id"]);
                        $bbs_account_data = array();
                        $bbs_account_data['total'] = $bbs_account['total'] + $order['product_num'];
                        $bbs_account_data['balance'] = $bbs_account['balance'] + $order['product_num'];
                        $this->bbs_account_model->update_bbs_account($bbs_account_data,$order['user_id']);
                        //更新充值订单附表   
                        $this->bbs_recharge_model->update_recharge_list($data,$order['order_nid']);
                        echo "success";	exit;
                    }else{
                        echo "fail";exit;	
                    }
                }
                echo "success";		//请不要修改或删除
            }else {
                //验证失败
                echo "fail";
            }
        }else{
            echo "fail";
        }
    }
    //检查充值订单是否支付完成
    function check_pay_order(){
        if($_POST){
            
            if(!$this->userinfo){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            
            $order_index = trim($_POST['order_index']);
            $rect_url = trim($_POST['rect_url']);
            //获取订单信息
            $order = $this->bbs_recharge_model->get_recharge_order(array("order_index"=>$order_index));
            if(!$order){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "订单信息获取失败";
                echo JSON($mess);exit;
            }
            if($order['status'] == 0 && $order['status'] != 1){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                echo JSON($mess);exit;
            }
            
            $mess['reqStatus'] = true;
            $mess['url'] = strpos($rect_url,"tan66") ? $rect_url : web_url("account");
            echo JSON($mess);exit;
        }
    }
    
    //处理打赏订单
    function action_award_order(){
        if($_POST){
            if(!$this->userinfo){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            $key = isset($_POST['awardKey']) ? trim($_POST['awardKey']) : "";
            $num = isset($_POST['awardNum']) ? trim($_POST['awardNum']) : 0;
            $pay = isset($_POST['awardPay']) ? trim($_POST['awardPay']) : "";
            $artId = isset($_POST['articleId']) ? trim($_POST['articleId']) : "";
            $autId = isset($_POST['authorId']) ? trim($_POST['authorId']) : "";
            $say = isset($_POST['awardSay']) ? trim($_POST['awardSay']) : "";
            
            $artId = decrypt_article_id($artId);
            $autId = decrypt_user_id($autId);
            
            if($key == "" || $key != $this->session_id){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            
            if(!$num){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "充值溜珠的个数需≥1";
                echo JSON($mess);exit;
            }
            
            if(!$pay || ($pay != "alipay" && $pay != "weixin" && $pay != "yue")){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "支付方式选择有误";
                echo JSON($mess);exit;
            }
            
            if($pay == "yue"){
                if($num > $this->bbs_userinfo['balance']){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1001;
                    $mess['errorMsg'] = "账户余额不足哦！";
                    echo JSON($mess);exit;
                }
            }
            
            
            //获取文章
            $article = $this->bbs_detail_model->get_article_read_num(array("id"=>$artId));
            if(!$article){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "非法参数";
                echo JSON($mess);exit;
            }
            
            if($autId != $article['authorid']){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "非法参数";
                echo JSON($mess);exit;
            }
            
            if($article['authorid'] == $this->bbs_userinfo['user_id'] || $autId == $this->bbs_userinfo['user_id']){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "不能给自己打赏哦";
                echo JSON($mess);exit;
            }
            
            $user_id = $this->bbs_userinfo['user_id'];
            $order_nid = MakeOrdId($user_id);//订单编号
            $data = array();
            $data['order_nid'] = $order_nid;
            $data['target_id'] = $artId;
            $data['target_type'] = 'article';
            $data['user_id'] = $user_id;
            $data['product_name'] = "打赏文章";
            $data['product_money'] = sprintf("%0.2f",$num);
            $data['product_num'] = $num;
            $data['pay_type'] = $pay;
            $data['status'] = 0;
            $data['create_time'] = time();
            $data['remark'] = $say ? $say : "这文章太棒了，犒劳一下，希望后续更加精彩";
            //创建打赏订单表
            $order_id = $this->bbs_recharge_model->insert_award_order($data);
            if($order_id){
                //创建打赏订单副表
                $this->bbs_recharge_model->insert_award_list($data);
                if($pay == "yue"){//余额支付
                    $result = $this->reward_by_yue($order_nid);
                    if($result){
                        
                        $mess['reqStatus'] = true;
                        $mess['errorMsg'] = "恭喜，打赏成功！";
                    }else{
                        $mess['reqStatus'] = false;
                        $mess['errorCode'] = 1001;
                        $mess['errorMsg'] = "操作失败，请稍后重试!";
                    }
                    echo JSON($mess);exit;
                }
                if($pay == "alipay"){//支付宝支付
                    $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$order_nid));
                    echo "正在跳转支付页面...";
                    //构造请求参数
                    $args = array();
                    $args['number'] = $order['order_nid'];
                    $args['name'] = $order['product_name'];
                    $args['total'] = $order['product_money'];
                    $args['content'] = "";
                    $args['notify_url'] = web_url("award_notify");
                    $args['return_url'] = web_url("award_result");
                    $alipay = new Alipay();
                    $alipay->recharge($this->alipay_config, $args);exit;
                }
                if($pay == "weixin"){//微信支付
                    
                }
                
            }
            
            
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1001;
            $mess['errorMsg'] = "非法请求";
            echo JSON($mess);exit;
        }
    }
    
    //余额支付处理
    private function reward_by_yue($order_nid){
        $order_nid = trim($order_nid);
        //获取打赏订单
        $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$order_nid));
        if($order['status'] == 1){
            return true;
        }
        $user_id = $order['user_id'];//打赏用户
        $article_id = $order['target_id'];//打赏文章
        $reward_num = $order['product_num'];//打赏数量
        
        //获取打赏用户账户
        $rewarder_bbs_account = $this->bbs_account_model->get($user_id);
        //获取打赏文章
        $reward_article = $this->bbs_detail_model->get_article_read_num(array("id"=>$article_id));
        //获取打赏文章作者账户
        $author_bbs_account = $this->bbs_account_model->get($reward_article['authorid']);
        
        //更新订单信息
        $data['status'] = 1;
        $data['update_time'] = time();
        if($this->bbs_recharge_model->update_award_order($data,$order_nid)){
            $this->bbs_recharge_model->update_award_list($data,$order_nid);
            //更新打赏者社区账户信息
            $rewarder_data = array();
            $rewarder_data['balance'] = $rewarder_bbs_account['balance'] - $reward_num;
            $rewarder_data['reward_expend'] = $rewarder_bbs_account['reward_expend'] + $reward_num;
            $rewarder_data['expend'] = $rewarder_bbs_account['expend'] + $reward_num;
            $this->bbs_account_model->update_bbs_account($rewarder_data,$user_id);
            //更新作者社区账户信息
            $rewarder_data = array();
            $rewarder_data['balance'] = $author_bbs_account['balance'] + $reward_num;
            $rewarder_data['total'] = $author_bbs_account['total'] + $reward_num;
            $rewarder_data['reward'] = $author_bbs_account['reward'] + $reward_num;
            $this->bbs_account_model->update_bbs_account($rewarder_data,$reward_article['authorid']);
            //添加打赏记录
            $reward = array();//insert_award
            $reward['user_id'] = $order['user_id'];
            $reward['target_id'] = $order['target_id'];
            $reward['target_type'] = 'article';
            $reward['order_id'] = $order['order_nid'];
            $reward['authorid'] = $reward_article['authorid'];
            $reward['reward_worth'] = $reward_num;
            $reward['award_say'] = $order['remark'];
            $reward['create_time'] = time();
            $reward['month'] = date("Ym");
            $reward['day'] = date("Ymd");
            
            $this->bbs_account_model->insert_award($reward);
            
            $this->db->insert("bbs_zhu_detail",array("user_id"=>$order["user_id"],"num"=>$reward_num,"type"=>"文章打赏支出","style"=>2,"create_time"=>time()));
            $this->db->insert("bbs_zhu_detail",array("user_id"=>$reward_article['authorid'],"num"=>$reward_num,"type"=>"文章打赏收入","style"=>1,"create_time"=>time()));
            return true;
        }else{
            return false;
        }
        
    }
    //打赏-页面跳转同步通知页面
    function award_result(){
        header("Content-type: text/html; charset=utf-8"); 
        $errorStatus = 0;$errorMsg = "";
        if($_GET){
            //计算得出通知验证结果
            $alipayNotify = new AlipayNotify($this->alipay_config);
            $verify_result = $alipayNotify->verifyReturn();
            
            if($verify_result) {//验证成功
                //商户订单号
                $out_trade_no = $_GET['out_trade_no'];
                if(!$out_trade_no){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1001;
                    $mess['errorMsg'] = "请求参数有误！";
                    echo JSON($mess);exit;
                }
                //支付宝交易号
                $trade_no = $_GET['trade_no'];
                //交易状态
                $trade_status = $_GET['trade_status'];

                $data = array();
                $data['trade_nid'] = $trade_no;
                $data['trade_status'] = $trade_status;
                //获取订单信息
                $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$out_trade_no));
                if(!$order){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1001;
                    $mess['errorMsg'] = "请求参数有误！";
                    echo JSON($mess);exit;
                }
                
                
                //获取打赏用户账户
                $rewarder_bbs_account = $this->bbs_account_model->get($order['user_id']);
                //获取打赏文章
                $reward_article = $this->bbs_detail_model->get_article_read_num(array("id"=>$order['target_id']));
                //获取打赏文章作者账户
                $author_bbs_account = $this->bbs_account_model->get($reward_article['authorid']);
                
                if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
                    if($order){
                        $data['status'] = 1;
                        $data['update_time'] = time();
                        if($order['status'] == 0){
                            //更新打赏订单
                            if($this->bbs_recharge_model->update_award_order($data,$order['order_nid'])){
                                //更新充值订单附表
                                $this->bbs_recharge_model->update_award_list($data,$order['order_nid']);
                                
                                
                                //更新打赏者社区账户信息
                                $rewarder_data = array();
                                $rewarder_data['reward_expend'] = $rewarder_bbs_account['reward_expend'] + $order['product_num'];
                                $rewarder_data['expend'] = $rewarder_bbs_account['expend'] + $order['product_num'];
                                $this->bbs_account_model->update_bbs_account($rewarder_data,$order['user_id']);
                                //更新作者社区账户信息
                                $rewarder_data = array();
                                $rewarder_data['balance'] = $author_bbs_account['balance'] + $order['product_num'];
                                $rewarder_data['total'] = $author_bbs_account['total'] + $order['product_num'];
                                $rewarder_data['reward'] = $author_bbs_account['reward'] + $order['product_num'];
                                $this->bbs_account_model->update_bbs_account($rewarder_data,$reward_article['authorid']);
                                //添加打赏记录
                                $reward = array();//insert_award
                                $reward['user_id'] = $order['user_id'];
                                $reward['target_id'] = $order['target_id'];
                                $reward['target_type'] = 'article';
                                $reward['order_id'] = $order['order_nid'];
                                $reward['authorid'] = $reward_article['authorid'];
                                $reward['reward_worth'] = $order['product_num'];
                                $reward['award_say'] = $order['remark'];
                                $reward['create_time'] = time();
                                $reward['month'] = date("Ym");
                                $reward['day'] = date("Ymd");
                                $this->bbs_account_model->insert_award($reward);
                                
                                $this->db->insert("bbs_zhu_detail",array("user_id"=>$order["user_id"],"num"=>$order['product_num'],"type"=>"文章打赏支出","style"=>2,"create_time"=>time()));
                                $this->db->insert("bbs_zhu_detail",array("user_id"=>$reward_article['authorid'],"num"=>$order['product_num'],"type"=>"文章打赏收入","style"=>1,"create_time"=>time()));
                                
                                $errorStatus = 0;
                                $errorMsg = "恭喜您，打赏成功！";
                            }else{
                                $errorStatus = 1;
                                $errorMsg = "操作失败，是稍后重试！";
                            }
                        }else{
                            $errorStatus = 0;
                            $errorMsg = "恭喜您，打赏成功！";
                        }
                        
                    }else{
                        $errorStatus = 1;
                        $errorMsg = "操作失败，原因：订单不存在！";
                    }
                    
                }else{
                    $errorStatus = 1;
                    $errorMsg =  "操作异常，异常状态码：".$_GET['trade_status'];
                }

            }else {
                $errorStatus = 1;
                $errorMsg =  "操作失败，原因：验签失败！";
            }
            
        }else{
            
        }
        
        $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$out_trade_no));
        $order['update_time'] = date("Y-m-d H:i:s",$order['update_time']);
        $order['detail_url'] = web_url("p/".encrypt_article_id($order['target_id']));
        
        $this->assign("errorStatus",$errorStatus);
        $this->assign("errorMsg", $errorMsg);
        $this->assign("order", $order);
        $this->display("recharge/award_result.html");
    }
    // 打赏-服务器异步通知页面路径
    function award_notify(){
        if($_POST){
            //计算得出通知验证结果
            $alipayNotify = new AlipayNotify($this->alipay_config);
            $verify_result = $alipayNotify->verifyNotify();
            if($verify_result) {//验证成功
                //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
                //商户订单号
                $out_trade_no = $_POST['out_trade_no'];
                //支付宝交易号
                $trade_no = $_POST['trade_no'];
                //交易状态
                $trade_status = $_POST['trade_status'];

                //获取订单信息
                $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$out_trade_no));
                
                if(!$order){
                    echo "fail";exit;
                }
                
                if($order['status'] == 1){//订单已经处理过了
                    echo "success";exit ;
                }
                
                //获取打赏用户账户
                $rewarder_bbs_account = $this->bbs_account_model->get($order['user_id']);
                //获取打赏文章
                $reward_article = $this->bbs_detail_model->get_article_read_num(array("id"=>$order['target_id']));
                //获取打赏文章作者账户
                $author_bbs_account = $this->bbs_account_model->get($reward_article['authorid']);
                
                $data = array();
                $data['trade_nid'] = $trade_no;
                $data['trade_status'] = $trade_status;
                $data['update_time'] = time();
                if($_POST['trade_status'] == 'TRADE_FINISHED' || $_POST['trade_status'] == 'TRADE_SUCCESS') {
                    $data['status'] = 1;
                    //更改充值订单状态
                    if($this->bbs_recharge_model->update_award_order($data,$order['order_nid'])){
                        //更新充值订单附表
                        $this->bbs_recharge_model->update_award_list($data,$order['order_nid']);

                        //更新打赏者社区账户信息
                        $rewarder_data = array();
                        $rewarder_data['reward_expend'] = $rewarder_bbs_account['reward_expend'] + $order['product_num'];
                        $rewarder_data['expend'] = $rewarder_bbs_account['expend'] + $order['product_num'];
                        $this->bbs_account_model->update_bbs_account($rewarder_data,$order['user_id']);
                        //更新作者社区账户信息
                        $rewarder_data = array();
                        $rewarder_data['balance'] = $author_bbs_account['balance'] + $order['product_num'];
                        $rewarder_data['total'] = $author_bbs_account['total'] + $order['product_num'];
                        $rewarder_data['reward'] = $author_bbs_account['reward'] + $order['product_num'];
                        $this->bbs_account_model->update_bbs_account($rewarder_data,$reward_article['authorid']);
                        //添加打赏记录
                        $reward = array();
                        $reward['user_id'] = $order['user_id'];
                        $reward['target_id'] = $order['target_id'];
                        $reward['target_type'] = 'article';
                        $reward['order_id'] = $order['order_nid'];
                        $reward['authorid'] = $reward_article['authorid'];
                        $reward['reward_worth'] = $order['product_num'];
                        $reward['award_say'] = $order['remark'];
                        $reward['create_time'] = time();
                        $reward['month'] = date("Ym");
                        $reward['day'] = date("Ymd");
                        $this->bbs_account_model->insert_award($reward);
                        
                        
                        $this->db->insert("bbs_zhu_detail",array("user_id"=>$order["user_id"],"num"=>$order['product_num'],"type"=>"文章打赏支出","style"=>2,"create_time"=>time()));
                        $this->db->insert("bbs_zhu_detail",array("user_id"=>$reward_article['authorid'],"num"=>$order['product_num'],"type"=>"文章打赏收入","style"=>1,"create_time"=>time()));
                        
                        echo "success";	exit;
                    }else{
                        echo "fail";exit;	
                    }
                }
                echo "success";		//请不要修改或删除
            }else {
                //验证失败
                echo "fail";
            }
        }else{
            echo "fail";
        }
    }
    //处理视频打赏订单
    function action_award_video_order(){
        if($_POST){
            if(!$this->userinfo){
                $mess['reqStatus'] = false;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            $key = isset($_POST['awardKey']) ? trim($_POST['awardKey']) : "";
            $num = isset($_POST['awardNum']) ? trim($_POST['awardNum']) : 0;
            $pay = isset($_POST['awardPay']) ? trim($_POST['awardPay']) : "";
            $videoId = isset($_POST['videoId']) ? trim($_POST['videoId']) : "";
            $say = isset($_POST['awardSay']) ? trim($_POST['awardSay']) : "";
            
            $videoId = decrypt_video_id($videoId);
            
            if($key == "" || $key != $this->session_id){
                $mess['reqStatus'] = false;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);exit;
            }
            
            if(!$num){
                $mess['reqStatus'] = false;
                $mess['errorMsg'] = "充值溜珠的个数需≥1";
                echo JSON($mess);exit;
            }
            
            if(!$pay || ($pay != "alipay" && $pay != "weixin" && $pay != "yue")){
                $mess['reqStatus'] = false;
                $mess['errorMsg'] = "支付方式选择有误";
                echo JSON($mess);exit;
            }
            
            if($pay == "yue"){
                if($num > $this->bbs_userinfo['balance']){
                    $mess['reqStatus'] = false;
                    $mess['errorMsg'] = "账户余额不足哦！";
                    echo JSON($mess);exit;
                }
            }
            
            //获取视频信息
            $video = $this->db->query("select * from tan_video where id = $videoId order by id desc limit 1")->row_array();
            if(!$video){
                $mess['reqStatus'] = false;
                $mess['errorMsg'] = "非法参数";
                echo JSON($mess);exit;
            }
            
            $user_id = $this->bbs_userinfo['user_id'];
            $order_nid = MakeOrdId($user_id);//订单编号
            $data = array();
            $data['order_nid'] = $order_nid;
            $data['target_id'] = $videoId;
            $data['target_type'] = "video";
            $data['user_id'] = $user_id;
            $data['product_name'] = "打赏视频";
            $data['product_money'] = sprintf("%0.2f",$num);
            $data['product_num'] = $num;
            $data['pay_type'] = $pay;
            $data['status'] = 0;
            $data['create_time'] = time();
            $data['remark'] = $say ? $say : "这个视频太棒了，犒劳一下，希望后续更加精彩";
            //创建打赏订单表
            $order_id = $this->bbs_recharge_model->insert_award_order($data);
            if($order_id){
                //创建打赏订单副表
                $this->bbs_recharge_model->insert_award_list($data);
                if($pay == "yue"){//余额支付
                    $result = $this->reward_video_by_yue($order_nid);
                    if($result){
                        
                        $mess['reqStatus'] = true;
                        $mess['errorMsg'] = "恭喜，打赏成功！";
                    }else{
                        $mess['reqStatus'] = false;
                        $mess['errorCode'] = 1001;
                        $mess['errorMsg'] = "操作失败，请稍后重试!";
                    }
                    echo JSON($mess);exit;
                }
                if($pay == "alipay"){//支付宝支付
                    $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$order_nid));
                    echo "正在跳转支付页面...";
                    //构造请求参数
                    $args = array();
                    $args['number'] = $order['order_nid'];
                    $args['name'] = $order['product_name'];
                    $args['total'] = $order['product_money'];
                    $args['content'] = "这里是产品描述";
                    $args['notify_url'] = web_url("award_v_notify");
                    $args['return_url'] = web_url("award_v_result");
                    $alipay = new Alipay();
                    $alipay->recharge($this->alipay_config, $args);exit;
                }
                if($pay == "weixin"){//微信支付
                    
                }
                
            }
            
            
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1001;
            $mess['errorMsg'] = "非法请求";
            echo JSON($mess);exit;
        }
    }
    //余额支付视频处理
    private function reward_video_by_yue($order_nid){
        $order_nid = trim($order_nid);
        //获取打赏订单
        $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$order_nid));
        if($order['status'] == 1){
            return true;
        }
        $user_id = $order['user_id'];//打赏用户
        $video_id = $order['target_id'];//打赏视频
        $reward_num = $order['product_num'];//打赏数量
        
        //获取打赏用户账户
        $rewarder_bbs_account = $this->bbs_account_model->get($user_id);
        //获取打赏视频
        $reward_video = $this->db->query("select * from tan_video where id = $video_id order by id desc limit 1")->row_array();
        //获取打赏文章作者账户
        $author_bbs_account = $this->bbs_account_model->get($reward_video['authorid']);
        
        //更新订单信息
        $data['status'] = 1;
        $data['update_time'] = time();
        if($this->bbs_recharge_model->update_award_order($data,$order_nid)){
            $this->bbs_recharge_model->update_award_list($data,$order_nid);
            //更新打赏者社区账户信息
            $rewarder_data = array();
            $rewarder_data['balance'] = $rewarder_bbs_account['balance'] - $reward_num;
            $rewarder_data['reward_expend'] = $rewarder_bbs_account['reward_expend'] + $reward_num;
            $rewarder_data['expend'] = $rewarder_bbs_account['expend'] + $reward_num;
            $this->bbs_account_model->update_bbs_account($rewarder_data,$user_id);
            //更新作者社区账户信息
            $rewarder_data = array();
            $rewarder_data['balance'] = $author_bbs_account['balance'] + $reward_num;
            $rewarder_data['total'] = $author_bbs_account['total'] + $reward_num;
            $rewarder_data['reward'] = $author_bbs_account['reward'] + $reward_num;
            $this->bbs_account_model->update_bbs_account($rewarder_data,$reward_video['authorid']);
            //添加打赏记录
            $reward = array();//insert_award
            $reward['user_id'] = $order['user_id'];
            $reward['target_id'] = $order['target_id'];
            $reward['target_type'] = $order['target_type'];
            $reward['order_id'] = $order['order_nid'];
            $reward['authorid'] = $reward_video['authorid'];
            $reward['reward_worth'] = $reward_num;
            $reward['award_say'] = $order['remark'];
            $reward['create_time'] = time();
            $reward['month'] = date("Ym");
            $reward['day'] = date("Ymd");
            
            $this->bbs_account_model->insert_award($reward);
            $this->db->query("update tan_video set reward_money = reward_money + $reward_num where id = $reward_video[id]");
            $this->db->insert("bbs_zhu_detail",array("user_id"=>$order["user_id"],"num"=>$reward_num,"type"=>"视频打赏支出","style"=>2,"create_time"=>time()));
            $this->db->insert("bbs_zhu_detail",array("user_id"=>$reward_video['authorid'],"num"=>$reward_num,"type"=>"视频打赏收入","style"=>1,"create_time"=>time()));
            return true;
        }else{
            return false;
        }
        
    }
    //视频打赏-页面跳转同步通知页面
    function award_video_result(){
        header("Content-type: text/html; charset=utf-8"); 
        $errorStatus = 0;$errorMsg = "";
        if($_GET){
            //计算得出通知验证结果
            $alipayNotify = new AlipayNotify($this->alipay_config);
            $verify_result = $alipayNotify->verifyReturn();
            
            if($verify_result) {//验证成功
                //商户订单号
                $out_trade_no = $_GET['out_trade_no'];
                if(!$out_trade_no){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1001;
                    $mess['errorMsg'] = "请求参数有误！";
                    echo JSON($mess);exit;
                }
                //支付宝交易号
                $trade_no = $_GET['trade_no'];
                //交易状态
                $trade_status = $_GET['trade_status'];

                $data = array();
                $data['trade_nid'] = $trade_no;
                $data['trade_status'] = $trade_status;
                //获取订单信息
                $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$out_trade_no));
                if(!$order){
                    $mess['reqStatus'] = false;
                    $mess['errorCode'] = 1001;
                    $mess['errorMsg'] = "请求参数有误！";
                    echo JSON($mess);exit;
                }
                
                
                //获取打赏用户账户
                $rewarder_bbs_account = $this->bbs_account_model->get($order['user_id']);
                //获取打赏视频
                $reward_video = $this->db->query("select * from tan_video where id = $order[target_id] order by id desc limit 1")->row_array();
                //获取打赏视频作者账户
                $author_bbs_account = $this->bbs_account_model->get($reward_video['authorid']);
                
                if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
                    if($order){
                        $data['status'] = 1;
                        $data['update_time'] = time();
                        if($order['status'] == 0){
                            //更新打赏订单
                            if($this->bbs_recharge_model->update_award_order($data,$order['order_nid'])){
                                //更新充值订单附表
                                $this->bbs_recharge_model->update_award_list($data,$order['order_nid']);
                                
                                
                                //更新打赏者社区账户信息
                                $rewarder_data = array();
                                $rewarder_data['reward_expend'] = $rewarder_bbs_account['reward_expend'] + $order['product_num'];
                                $rewarder_data['expend'] = $rewarder_bbs_account['expend'] + $order['product_num'];
                                $this->bbs_account_model->update_bbs_account($rewarder_data,$order['user_id']);
                                //更新作者社区账户信息
                                $rewarder_data = array();
                                $rewarder_data['balance'] = $author_bbs_account['balance'] + $order['product_num'];
                                $rewarder_data['total'] = $author_bbs_account['total'] + $order['product_num'];
                                $rewarder_data['reward'] = $author_bbs_account['reward'] + $order['product_num'];
                                $this->bbs_account_model->update_bbs_account($rewarder_data,$reward_video['authorid']);
                                //添加打赏记录
                                $reward = array();//insert_award
                                $reward['user_id'] = $order['user_id'];
                                $reward['target_id'] = $order['target_id'];
                                $reward['target_type'] = $order['target_type'];
                                $reward['order_id'] = $order['order_nid'];
                                $reward['authorid'] = $reward_video['authorid'];
                                $reward['reward_worth'] = $order['product_num'];
                                $reward['award_say'] = $order['remark'];
                                $reward['create_time'] = time();
                                $reward['month'] = date("Ym");
                                $reward['day'] = date("Ymd");
                                $this->bbs_account_model->insert_award($reward);
                                $this->db->query("update tan_video set reward_money = reward_money + $order[product_num] where id = $reward_video[id]");
                                $this->db->insert("bbs_zhu_detail",array("user_id"=>$order["user_id"],"num"=>$order['product_num'],"type"=>"视频打赏支出","style"=>2,"create_time"=>time()));
                                $this->db->insert("bbs_zhu_detail",array("user_id"=>$reward_video['authorid'],"num"=>$order['product_num'],"type"=>"视频打赏收入","style"=>1,"create_time"=>time()));
                                
                                $errorStatus = 0;
                                $errorMsg = "恭喜您，打赏成功！";
                            }else{
                                $errorStatus = 1;
                                $errorMsg = "操作失败，是稍后重试！";
                            }
                        }else{
                            $errorStatus = 0;
                            $errorMsg = "恭喜您，打赏成功！";
                        }
                        
                    }else{
                        $errorStatus = 1;
                        $errorMsg = "操作失败，原因：订单不存在！";
                    }
                    
                }else{
                    $errorStatus = 1;
                    $errorMsg =  "操作异常，异常状态码：".$_GET['trade_status'];
                }

            }else {
                $errorStatus = 1;
                $errorMsg =  "操作失败，原因：验签失败！";
            }
            
        }else{
            
        }
        
        $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$out_trade_no));
        $order['update_time'] = date("Y-m-d H:i:s",$order['update_time']);
        $order['detail_url'] = web_url("video/".$reward_video['salt']);
        
        $this->assign("errorStatus",$errorStatus);
        $this->assign("errorMsg", $errorMsg);
        $this->assign("order", $order);
        $this->display("recharge/award_result.html");
    }
    // 视频打赏-服务器异步通知页面路径
    function award_video_notify(){
        if($_POST){
            //计算得出通知验证结果
            $alipayNotify = new AlipayNotify($this->alipay_config);
            $verify_result = $alipayNotify->verifyNotify();
            if($verify_result) {//验证成功
                //获取支付宝的通知返回参数，可参考技术文档中服务器异步通知参数列表
                //商户订单号
                $out_trade_no = $_POST['out_trade_no'];
                //支付宝交易号
                $trade_no = $_POST['trade_no'];
                //交易状态
                $trade_status = $_POST['trade_status'];

                //获取订单信息
                $order = $this->bbs_recharge_model->get_award_order(array("order_nid"=>$out_trade_no));
                
                if(!$order){
                    echo "fail";exit;
                }
                
                if($order['status'] == 1){//订单已经处理过了
                    echo "success";exit ;
                }
                
                //获取打赏用户账户
                $rewarder_bbs_account = $this->bbs_account_model->get($order['user_id']);
                //获取打赏视频
                $reward_video =  $this->db->query("select * from tan_video where id = $order[target_id] order by id desc limit 1")->row_array();
                //获取打赏视频作者账户
                $author_bbs_account = $this->bbs_account_model->get($reward_video['authorid']);
                
                $data = array();
                $data['trade_nid'] = $trade_no;
                $data['trade_status'] = $trade_status;
                $data['update_time'] = time();
                if($_POST['trade_status'] == 'TRADE_FINISHED' || $_POST['trade_status'] == 'TRADE_SUCCESS') {
                    $data['status'] = 1;
                    //更改充值订单状态
                    if($this->bbs_recharge_model->update_award_order($data,$order['order_nid'])){
                        //更新充值订单附表
                        $this->bbs_recharge_model->update_award_list($data,$order['order_nid']);

                        //更新打赏者社区账户信息
                        $rewarder_data = array();
                        $rewarder_data['reward_expend'] = $rewarder_bbs_account['reward_expend'] + $order['product_num'];
                        $rewarder_data['expend'] = $rewarder_bbs_account['expend'] + $order['product_num'];
                        $this->bbs_account_model->update_bbs_account($rewarder_data,$order['user_id']);
                        //更新作者社区账户信息
                        $rewarder_data = array();
                        $rewarder_data['balance'] = $author_bbs_account['balance'] + $order['product_num'];
                        $rewarder_data['total'] = $author_bbs_account['total'] + $order['product_num'];
                        $rewarder_data['reward'] = $author_bbs_account['reward'] + $order['product_num'];
                        $this->bbs_account_model->update_bbs_account($rewarder_data,$reward_video['authorid']);
                        //添加打赏记录
                        $reward = array();
                        $reward['user_id'] = $order['user_id'];
                        $reward['target_id'] = $order['target_id'];
                        $reward['target_type'] = $order['target_type'];
                        $reward['order_id'] = $order['order_nid'];
                        $reward['authorid'] = $reward_video['authorid'];
                        $reward['reward_worth'] = $order['product_num'];
                        $reward['award_say'] = $order['remark'];
                        $reward['create_time'] = time();
                        $reward['month'] = date("Ym");
                        $reward['day'] = date("Ymd");
                        $this->bbs_account_model->insert_award($reward);
                        
                        $this->db->query("update tan_video set reward_money = reward_money + $order[product_num] where id = $reward_video[id]");
                        $this->db->insert("bbs_zhu_detail",array("user_id"=>$order["user_id"],"num"=>$order['product_num'],"type"=>"视频打赏支出","style"=>2,"create_time"=>time()));
                        $this->db->insert("bbs_zhu_detail",array("user_id"=>$reward_video['authorid'],"num"=>$order['product_num'],"type"=>"视频打赏收入","style"=>1,"create_time"=>time()));
                        
                        echo "success";	exit;
                    }else{
                        echo "fail";exit;	
                    }
                }
                echo "success";		//请不要修改或删除
            }else {
                //验证失败
                echo "fail";
            }
        }else{
            echo "fail";
        }
    }
    
}
